import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { MenuClosure, MenuEntity } from "./entity/menu.entity";
import { CatalogueVO } from "./vo/menu.vo";

@Injectable()
export class MenuRepositry {
  constructor(
    @InjectRepository(MenuEntity) private readonly menuRepository: Repository<MenuEntity>
  ) { }

  /**
   * 查询所有目录
   * @returns 目录扁平数组
   */
  // : Promise<CatalogueVO>
  async getCatalogue() {
    return await this.menuRepository.createQueryBuilder('menu')
      .leftJoin(MenuClosure, 'closure', "menu.id = closure.descendantId")
      .select(["menu.id", "menu.menuName"])
      .addSelect("closure.ancestorId", "menu_parentId") // 映射虚拟字段,语法：表名_字段名
      .where(`menu.delete_flag = ${false}`)
      .getMany()

    return this.menuRepository.query(`
      SELECT 
      a.id,a.menu_name,b.ancestor_id
       from test_menu a 
       LEFT JOIN test_menu_closure b on a.id = b.descendant_id 
       where a.delete_flag = false and a.type = 'subMenu'
      `)
  }

  addMenu() {

  }

}